package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.model.Enum.RoleType;
import org.javaweb.cms_web.model.Enum.UserStatus;
import org.javaweb.cms_web.model.LoginTicket;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.service.UserService;
import org.javaweb.cms_web.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private HostHolder hostHolder;

    // 显示登录页面
    @GetMapping("/login")
    public String loginPage() {
        return "login"; // 返回 login.jsp 页面
    }

    // 处理登录请求
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model, HttpServletResponse response) {
        // 使用用户名和密码进行验证
        User user = userService.authenticate(username, password);

        if (user != null && (user.getStatus() == UserStatus.PASSED.getCode()||user.getStatus() == UserStatus.TEAM_PENDING_REVIEW.getCode()||user.getStatus() == UserStatus.TEAM_NOT_PASSED.getCode())) {
            Map<String, Object> loginTicket = userService.Login(user);
            if (loginTicket.containsKey("ticket")) {
                Cookie cookie = new Cookie("ticket", loginTicket.get("ticket").toString());
                cookie.setPath("/");
                cookie.setMaxAge(3600);
                response.addCookie(cookie);
            }
            // 添加 roleType 判断逻辑
            if (user.getRoleType() == RoleType.VISITOR.getCode()) {
                // 普通用户
                model.addAttribute("user", user);
                return "redirect:/dashboard"; // 跳转到普通用户页面
            }
        } else {
            // 登录失败，返回登录页并显示错误
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
        //根据用户角色跳转到不同的页面
        switch (user.getRoleType()) {
            case 0:
                return "redirect:/superAdmin/achievementReview";
            case 1:
                return "redirect:/admin/teamadmin/achievementmanagement";
            case 2:
                return "redirect:/team-member/info";
            case 3:
                return "redirect:/visitor.jsp";
        }
        return "login";
    }

    // 处理登出请求
    @GetMapping("/logout")
    public String logout() {
        // 检查用户是否已登录
        LoginTicket loginTicket = hostHolder.getLoginTicket();
        if (loginTicket == null || loginTicket.getStatus() == 1) {
            // 如果未登录或登录票据失效，跳转到未登录提示页面
            return "notLoggedIn";
        }
        userService.Logout();
        return "redirect:/login"; // 跳转回登录页面
    }
}